//
//  JXHCustomCollectionView.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/10/25.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit
import SDWebImage

class JXHCustomCollectionView: UICollectionView {

    var pics : [URL] = [URL]() {
        didSet{
            self.reloadData()
        }
    }
    
    
    override func awakeFromNib() {
        
        super.awakeFromNib()
        
        dataSource = self
        delegate = self
    }
}

extension JXHCustomCollectionView : UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pics.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let  cell = collectionView.dequeueReusableCell(withReuseIdentifier: "picCell", for: indexPath) as! JXHPicCollectionViewCell
        cell.picURL = pics[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     
        //通知
        
        let userInfo : [String : Any] = ["indexPath" : indexPath, "picURLs" : pics]
    
        let noteName = NSNotification.Name.init(showPhotoBrowserNote)
        
        NotificationCenter.default.post(name: noteName, object: self, userInfo: userInfo)
    }
}

extension JXHCustomCollectionView : JXHPhotoBrowserAnimatorPresentDelegate {
    
    func startRect(indexPath : IndexPath) -> CGRect? {
    
        let cell = self.cellForItem(at: indexPath)!
        let startFrame = self.convert(cell.frame, to: UIApplication.shared.keyWindow!)
        return startFrame
    }
    func endRect(indexPath : IndexPath) -> CGRect? {
    
        let picURL = pics[indexPath.item]
        
        guard let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picURL.absoluteString) else {
            return nil
        }
        let w = UIScreen.main.bounds.width
        
        let h = w / image.size.width * image.size.height
        
        var y : CGFloat = 0
        
        if h > UIScreen.main.bounds.height {
            y = 0
        }else{
            y = (UIScreen.main.bounds.height - h) * 0.5
        }
        
        return CGRect.init(x: 0, y: y, width: w, height: h)
    }
    func imageView(indexPath : IndexPath) -> UIImageView? {
    
        let imageView = UIImageView()
        
        let picURL = pics[indexPath.item]
        
        guard let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: picURL.absoluteString) else {
            return nil
        }
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }
}


class JXHPicCollectionViewCell: UICollectionViewCell {
    
    var picURL : URL? {
        didSet{
            guard let picURL = picURL else {
                return
            }
            iconIamgeView.sd_setImage(with: picURL, placeholderImage: UIImage.init(named: ""))
        }
    }
    
    
    @IBOutlet weak var iconIamgeView: UIImageView!

}
